﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace InstaMe.Cores
{
    public class CoreSettings : INotifyPropertyChanged
    {
        private static CoreSettings mInstance = null;
        private static object mLock = new object();
        private static IsolatedStorageSettings mAppSetting = IsolatedStorageSettings.ApplicationSettings;

        public const string ABOUT_VERSION = "1.0.0";
        public static string DISPLAY_NAME = "";
        public static string USER_AVATAR = "";
        public static string USER_STATUS = "";
        public static long USER_ID = 0;
        public static bool SHOW_MEM = true;

        private const string C_USER_ID = "userid";
        private const string C_DISPLAY_NAME = "displayname";
        private const string C_AVATAR = "avatar";
        private const string C_STATUS = "status";

        public CoreSettings()
        {
        
        }

        public static CoreSettings getInstance()
        {
            if (mInstance == null)
            {
                lock (mLock)
                {
                    if (mInstance == null)
                    {
                        mInstance = new CoreSettings();
                    }
                }
            }
            return mInstance;
        }

        public static void SAVE_USER_INFO()
        {
            AddOrUpdateSetting(C_USER_ID, USER_ID);
            AddOrUpdateSetting(C_DISPLAY_NAME, DISPLAY_NAME);
            AddOrUpdateSetting(C_AVATAR, USER_AVATAR);
            AddOrUpdateSetting(C_STATUS, USER_STATUS);
            Save();
        }

        public static void LOAD_USER_INFO()
        {
            CoreSettings.USER_ID = GetSetting<long>(C_USER_ID, 0);
            CoreSettings.DISPLAY_NAME = GetSetting<string>(C_DISPLAY_NAME, "");
            CoreSettings.USER_AVATAR = GetSetting<string>(C_AVATAR, "");
            CoreSettings.USER_STATUS = GetSetting<string>(C_STATUS, "");
        }

        public static void CLEAR_USER_INFO()
        {
            USER_ID = 0;
            DISPLAY_NAME = "";
            USER_AVATAR = "";
            USER_STATUS = "";
        }

        private static void Save()
        {
            lock (mAppSetting)
            {
                mAppSetting.Save();
            }
        }

        private static void AddOrUpdateSetting(string aSettingName, object aValue)
        {
            lock (mAppSetting)
            {
                if (mAppSetting.Contains(aSettingName))
                {
                    mAppSetting[aSettingName] = aValue;
                }
                else
                {
                    mAppSetting.Add(aSettingName, aValue);
                }
            }
        }

        private static TValue GetSetting<TValue>(string aSettingName, TValue aDefaultValue)
        {
            lock (mAppSetting)
            {
                if (mAppSetting.Contains(aSettingName))
                {
                    TValue iReturnValue = (TValue)mAppSetting[aSettingName];
                    return iReturnValue;
                }
                else
                {
                    return aDefaultValue;
                }
            }
        }

        // MODEL
        public long MODEL_USER_ID
        {
            get
            {
                return USER_ID;
            }
            set
            {
                USER_ID = value;
                NotifyPropertyChanged("MODEL_USER_ID");
            }
        }

        public string MODEL_DISPLAY_NAME
        {
            get
            {
                return DISPLAY_NAME;
            }
            set 
            {
                DISPLAY_NAME = value;
                NotifyPropertyChanged("MODEL_DISPLAY_NAME");
            }
        }

        public string MODEL_USER_AVATAR
        {
            get
            {
                return USER_AVATAR.Length > 0 ? USER_AVATAR : "DefaultAvatarHere";
            }
            set
            {
                USER_AVATAR = value;
                NotifyPropertyChanged("MODEL_USER_AVATAR");
            }
        }

        public string MODEL_USER_STATUS
        {
            get
            {
                return USER_STATUS;
            }
            set
            {
                USER_STATUS = value;
                NotifyPropertyChanged("MODEL_USER_STATUS");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (null != handler)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            });
        }
    }
}
